package com.shenmazong.order.controller;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.shenmazong.utils.AliyunOssUtil;
import com.shenmazong.utils.FastFileUtil;
import com.shenmazong.utils.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author 军哥
 * @version 1.0
 * @description: TbUploadController
 * @date 2022/8/6 10:06
 */

@RestController
@Slf4j
@RequestMapping(value = "/upload")
public class TbUploadController {

//    @Autowired
//    private FastFileStorageClient storageClient;

    @Autowired
    private FastFileUtil fastFileUtil;

    private String serverUrl = "http://8.142.83.78:8888/";

    @PostMapping(value = "/ossUpload")
//    public ResultResponse ossUpload(@RequestParam("file")MultipartFile file) {
    public ResultResponse ossUpload(@RequestPart("file")MultipartFile file) throws IOException {
        //--1 上传OSS
        String url = AliyunOssUtil.OSS().upload(file);

        //--2 备份fastdfs
//        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
//                FilenameUtils.getExtension(file.getOriginalFilename()), null);
//        String fullPath = storePath.getFullPath();
//        String fastUrl = serverUrl + fullPath;
        String fastUrl = fastFileUtil.uploadFile(file);


        //--2 把url存入到数据库中

        //--3 返回文件信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("ossURl", url);
        map.put("fastUrl", fastUrl);

        return ResultResponse.SUCCESS(map);
    }
}
